﻿using System;
using System.Collections.Generic;
using Eassets.Layered.Domain.Global;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;
using Eassets.Layered.Domain;

namespace Eassets.Data.Global
{
    public class AuthorizationGroupMemberData
    {
        /// <summary>
        /// Inserts authorizationGroupMember.
        /// </summary>
        /// <param name="authorizationGroupMember"></param>
        public void AddAuthorizationGroupMember(AuthorizationGroupMember authorizationGroupMember)
        {
            SqlCommand command = new SqlCommand("SP_AddAuthorizationGroupMember"); // Name of the SP
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, authorizationGroupMember));
        }

        /// <summary>
        /// Updates authorizationGroupMember.
        /// </summary>
        /// <param name="authorizationGroupMember"></param>
        public void ModifyAuthorizationGroupMember(AuthorizationGroupMember authorizationGroupMember)
        {
            SqlCommand command = new SqlCommand("SP_ModifyAuthorizationGroupMember"); // Name of the SP
            command.Parameters.AddWithValue("@AuthorizationGroupMemberId", authorizationGroupMember.AuthorizationGroupMemberId);
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, authorizationGroupMember));
        }


        /// <summary>
        /// Delete authorizationGroupMember.
        /// </summary>
        /// <param name="authorizationGroupMemberId"></param>
        public void DeleteAuthorizationGroupMember(long authorizationGroupMemberId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteAuthorizationGroupMember"); // Name of the SP
            command.Parameters.AddWithValue("@AuthorizationGroupMemberId", authorizationGroupMemberId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId authorizationGroupMember.
        /// </summary>
        /// <param name="authorizationGroupMemberId"></param>
        /// <returns></returns>
        public AuthorizationGroupMember GetByIdAuthorizationGroupMember(long authorizationGroupMemberId)
        {
            SqlCommand command = new SqlCommand("SP_GetAuthorizationGroupMemberById");
            command.Parameters.AddWithValue("@AuthorizationGroupMemberId", authorizationGroupMemberId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AuthorizationGroupMember authorizationGroupMember = new AuthorizationGroupMember();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["AuthorizationGroupMemberId"].ToString())))
                {
                    authorizationGroupMember.AuthorizationGroupMemberId = Convert.ToInt64(dataReader["AuthorizationGroupMemberId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AuthorizationGroupId"].ToString())))
                {
                    authorizationGroupMember.AuthorizationGroupId = Convert.ToInt64(dataReader["AuthorizationGroupId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["EmployeeId"].ToString())))
                {
                    authorizationGroupMember.EmployeeId = Convert.ToInt64(dataReader["EmployeeId"]);
                }
            }
            dataReader.Close();
            return authorizationGroupMember;
        }

        /// <summary>
        /// GetAll authorizationGroupMember.
        /// </summary>
        /// <returns></returns>
        public Collection<AuthorizationGroupMember> GetAllAuthorizationGroupMember()
        {
            Collection<AuthorizationGroupMember> authorizationGroupMembers = new Collection<AuthorizationGroupMember>();
            SqlCommand command = new SqlCommand("SP_GetAllAuthorizationGroupMembers");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);
            AuthorizationGroupMember authorizationGroupMember;
            while (dataReader.Read())
            {
                authorizationGroupMember = new AuthorizationGroupMember();
                if (!(String.IsNullOrEmpty(dataReader["AuthorizationGroupMemberId"].ToString())))
                {
                    authorizationGroupMember.AuthorizationGroupMemberId = Convert.ToInt64(dataReader["AuthorizationGroupMemberId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AuthorizationGroupId"].ToString())))
                {
                    authorizationGroupMember.AuthorizationGroupId = Convert.ToInt64(dataReader["AuthorizationGroupId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["EmployeeId"].ToString())))
                {
                    authorizationGroupMember.EmployeeId = Convert.ToInt64(dataReader["EmployeeId"]);
                }

                authorizationGroupMembers.Add(authorizationGroupMember);
            }
            dataReader.Close();
            return authorizationGroupMembers;
        }

        public Collection<AuthorizationGroupMember> GetmappedEmployeeDetailsByAuthorizationGroupId(long AuthorizationGroupId)
        {
            Collection<AuthorizationGroupMember> authorizationGroupMembers = new Collection<AuthorizationGroupMember>();
            SqlCommand command = new SqlCommand("SP_GetmappedEmployeeDetailsByAuthorizationGroupId");
            command.Parameters.AddWithValue("@AuthorizationGroupId", AuthorizationGroupId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AuthorizationGroupMember authorizationGroupMember;
            while (dataReader.Read())
            {
                authorizationGroupMember = new AuthorizationGroupMember();

                if (!(String.IsNullOrEmpty(dataReader["EmployeeId"].ToString())))
                {
                    authorizationGroupMember.EmployeeId = Convert.ToInt64(dataReader["EmployeeId"]);
                }
                EmployeeData employeeData = new EmployeeData();
                Employee employee = new Employee();
                employee = employeeData.GetByIdEmployee(authorizationGroupMember.EmployeeId);

                authorizationGroupMembers.Add(authorizationGroupMember);
            }
            dataReader.Close();
            return authorizationGroupMembers;
        }

        public Collection<AuthorizationGroupMember> GetAllUnmappedEmployeeDetailsByAuthorizationGroupId(long AuthorizationGroupId)
        {
            Collection<AuthorizationGroupMember> authorizationGroupMembers = new Collection<AuthorizationGroupMember>();
            SqlCommand command = new SqlCommand("SP_GetUnmappedEmployeeDetailsByAuthorizationGroupId");
            command.Parameters.AddWithValue("@AuthorizationGroupId", AuthorizationGroupId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AuthorizationGroupMember authorizationGroupMember;
            while (dataReader.Read())
            {
                authorizationGroupMember = new AuthorizationGroupMember();

                if (!(String.IsNullOrEmpty(dataReader["EmployeeId"].ToString())))
                {
                    authorizationGroupMember.EmployeeId = Convert.ToInt64(dataReader["EmployeeId"]);
                }
                EmployeeData employeeData = new EmployeeData();
                Employee employee = new Employee();
                employee = employeeData.GetByIdEmployee(authorizationGroupMember.EmployeeId);

                if (!(String.IsNullOrEmpty(dataReader["EmployeeName"].ToString())))
                {
                    employee.EmployeeName = Convert.ToString(dataReader["EmployeeName"]);
                }
                authorizationGroupMembers.Add(authorizationGroupMember);
            }
            dataReader.Close();
            return authorizationGroupMembers;
        }

        public Collection<long> GetEmpIdFromAuthorizationGroupMember(long authorizationGroupId)
        {
            Collection<long> empIdList = new Collection<long>();
            SqlCommand command = new SqlCommand("SP_GetEmpIdFromAuthorizationGroupMember");
            command.Parameters.AddWithValue("AuthorizationGroupId", authorizationGroupId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);
            while (dataReader.Read())
            {
                long id = new long();
                if (!(String.IsNullOrEmpty(dataReader["EmployeeId"].ToString())))
                {
                    id = Convert.ToInt64(dataReader["EmployeeId"]);
                }

                empIdList.Add(id);
            }
            dataReader.Close();
            return empIdList;
        }

        public Collection<long> GetUnMappedEmpIdsByAuthorizationGroup(long authorizationGroupId)
        {
            Collection<long> empIdList = new Collection<long>();
            SqlCommand command = new SqlCommand("SP_GetUnMappedEmpIdsByAuthorizationGroup");
            command.Parameters.AddWithValue("@AuthorizationGroupId", authorizationGroupId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);
            while (dataReader.Read())
            {
                long id = new long();
                if (!(String.IsNullOrEmpty(dataReader["EmployeeId"].ToString())))
                {
                    id = Convert.ToInt64(dataReader["EmployeeId"]);
                }
                empIdList.Add(id);
            }
            dataReader.Close();
            return empIdList;
        }

        public long GetAuthorizationGroupMemberIdFromAuthorizationGroupMember(long employeeId, long authorizationGroupId)
        {
            long authorizationGroupMemberId = 0;
            SqlCommand command = new SqlCommand("SP_GetAuthorizationGroupMemberIdFromAuthorizationGroupMember");
            command.Parameters.AddWithValue("EmployeeId", employeeId);
            command.Parameters.AddWithValue("AuthorizationGroupId", authorizationGroupId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData();
            SqlDataReader dataReader = commonData.ExecuteReader(command);
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["AuthorizationGroupMemberId"].ToString())))
                {
                    authorizationGroupMemberId = Convert.ToInt64(dataReader["AuthorizationGroupMemberId"]);
                }
            }
            return authorizationGroupMemberId;
        }

        public void Map(List<AuthorizationGroupMember> authorizationGroupMembers)
        {
            foreach (AuthorizationGroupMember authorizationGroupMember in authorizationGroupMembers)
            {
                this.AddAuthorizationGroupMember(authorizationGroupMember);
            }

        }

        public SqlCommand ConstructCommand(SqlCommand command, AuthorizationGroupMember authorizationGroupMember)
        {
            command.CommandType = CommandType.StoredProcedure;


            try
            {
                command.Parameters.AddWithValue("@AuthorizationGroupId", authorizationGroupMember.AuthorizationGroupId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@EmployeeId", authorizationGroupMember.EmployeeId);
            }
            catch (Exception)
            { }
            return command;
        }


        //public Collection<long> GetEmpIdFromEmployeeMappedByAuthorizationGroupMember()
        //{
        //    Collection<long> empIdList = new Collection<long>();
        //    SqlCommand command = new SqlCommand("SP_GetEmpIdFromEmployeeMappedByAuthorizationGroupMember");
        //    command.CommandType = CommandType.StoredProcedure;
        //    CommonData commonData = new CommonData();
        //    SqlDataReader dataReader = commonData.ExecuteReader(command);
        //    while (dataReader.Read())
        //    {
        //        long id = new long();
        //        if (!(String.IsNullOrEmpty(dataReader["EmployeeId"].ToString())))
        //        {
        //            id = Convert.ToInt64(dataReader["EmployeeId"]);
        //        }
        //        empIdList.Add(id);
        //    }
        //    dataReader.Close();
        //    return empIdList;
        //}


    }

}
